



<!doctype html>
<html lang="en" class="no-js">
  <head>
    
      <meta charset="utf-8">
      <meta name="viewport" content="width=device-width,initial-scale=1">
      <meta http-equiv="x-ua-compatible" content="ie=edge">
      
        <meta name="description" content="Here you can find all documents about QTrobot.">
      
      
        <link rel="canonical" href="https://luxai-qtrobot.github.io/demos/qt_microphone_detection/">
      
      
        <meta name="author" content="QTrobot">
      
      
        <meta name="lang:clipboard.copy" content="Copy to clipboard">
      
        <meta name="lang:clipboard.copied" content="Copied to clipboard">
      
        <meta name="lang:search.language" content="en">
      
        <meta name="lang:search.pipeline.stopwords" content="True">
      
        <meta name="lang:search.pipeline.trimmer" content="True">
      
        <meta name="lang:search.result.none" content="No matching documents">
      
        <meta name="lang:search.result.one" content="1 matching document">
      
        <meta name="lang:search.result.other" content="# matching documents">
      
        <meta name="lang:search.tokenizer" content="[\s\-]+">
      
      <link rel="shortcut icon" href="../../img/luxai-favicon.ico">
      <meta name="generator" content="mkdocs-1.1, mkdocs-material-4.6.3">
    
    
      
        <title>**Microphone Voice Activity & Direction Detection** - QTrobot Documentation</title>
      
    
    
      <link rel="stylesheet" href="../../assets/stylesheets/application.adb8469c.css">
      
        <link rel="stylesheet" href="../../assets/stylesheets/application-palette.a8b3c06d.css">
      
      
        
        
        <meta name="theme-color" content="#3f51b5">
      
    
    
      <script src="../../assets/javascripts/modernizr.86422ebf.js"></script>
    
    
      
        <link href="https://fonts.gstatic.com" rel="preconnect" crossorigin>
        <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto:300,400,400i,700%7CRoboto+Mono&display=fallback">
        <style>body,input{font-family:"Roboto","Helvetica Neue",Helvetica,Arial,sans-serif}code,kbd,pre{font-family:"Roboto Mono","Courier New",Courier,monospace}</style>
      
    
    <link rel="stylesheet" href="../../assets/fonts/material-icons.css">
    
    
      <link rel="stylesheet" href="../../stylesheets/extra.css">
    
    
      
    
    
  </head>
  
    
    
    <body dir="ltr" data-md-color-primary="indigo" data-md-color-accent="indigo">
  
    <svg class="md-svg">
      <defs>
        
        
      </defs>
    </svg>
    <input class="md-toggle" data-md-toggle="drawer" type="checkbox" id="__drawer" autocomplete="off">
    <input class="md-toggle" data-md-toggle="search" type="checkbox" id="__search" autocomplete="off">
    <label class="md-overlay" data-md-component="overlay" for="__drawer"></label>
    
      <a href="#microphone-voice-activity-direction-detection" tabindex="0" class="md-skip">
        Skip to content
      </a>
    
    
      <header class="md-header" data-md-component="header">
  <nav class="md-header-nav md-grid">
    <div class="md-flex">
      <div class="md-flex__cell md-flex__cell--shrink">
        <a href="https://luxai-qtrobot.github.io/" title="QTrobot Documentation" aria-label="QTrobot Documentation" class="md-header-nav__button md-logo">
          
            <img alt="logo" src="../../img/logo_luxai_white.png" width="24" height="24">
          
        </a>
      </div>
      <div class="md-flex__cell md-flex__cell--shrink">
        <label class="md-icon md-icon--menu md-header-nav__button" for="__drawer"></label>
      </div>
      <div class="md-flex__cell md-flex__cell--stretch">
        <div class="md-flex__ellipsis md-header-nav__title" data-md-component="title">
          
            <span class="md-header-nav__topic">
              QTrobot Documentation
            </span>
            <span class="md-header-nav__topic">
              
                **Microphone Voice Activity & Direction Detection**
              
            </span>
          
        </div>
      </div>
      <div class="md-flex__cell md-flex__cell--shrink">
        
          <label class="md-icon md-icon--search md-header-nav__button" for="__search"></label>
          
<div class="md-search" data-md-component="search" role="dialog">
  <label class="md-search__overlay" for="__search"></label>
  <div class="md-search__inner" role="search">
    <form class="md-search__form" name="search">
      <input type="text" class="md-search__input" aria-label="search" name="query" placeholder="Search" autocapitalize="off" autocorrect="off" autocomplete="off" spellcheck="false" data-md-component="query" data-md-state="active">
      <label class="md-icon md-search__icon" for="__search"></label>
      <button type="reset" class="md-icon md-search__icon" data-md-component="reset" tabindex="-1">
        &#xE5CD;
      </button>
    </form>
    <div class="md-search__output">
      <div class="md-search__scrollwrap" data-md-scrollfix>
        <div class="md-search-result" data-md-component="result">
          <div class="md-search-result__meta">
            Type to start searching
          </div>
          <ol class="md-search-result__list"></ol>
        </div>
      </div>
    </div>
  </div>
</div>
        
      </div>
      
    </div>
  </nav>
</header>
    
    <div class="md-container">
      
        
      
      
      <main class="md-main" role="main">
        <div class="md-main__inner md-grid" data-md-component="container">
          
            
              <div class="md-sidebar md-sidebar--primary" data-md-component="navigation">
                <div class="md-sidebar__scrollwrap">
                  <div class="md-sidebar__inner">
                    <nav class="md-nav md-nav--primary" data-md-level="0">
  <label class="md-nav__title md-nav__title--site" for="__drawer">
    <a href="https://luxai-qtrobot.github.io/" title="QTrobot Documentation" class="md-nav__button md-logo">
      
        <img alt="logo" src="../../img/logo_luxai_white.png" width="48" height="48">
      
    </a>
    QTrobot Documentation
  </label>
  
  <ul class="md-nav__list" data-md-scrollfix>
    
      
      
      


  <li class="md-nav__item">
    <a href="../.." title="Home" class="md-nav__link">
      Home
    </a>
  </li>

    
      
      
      


  <li class="md-nav__item">
    <a href="../../user-manual/" title="Quick Start" class="md-nav__link">
      Quick Start
    </a>
  </li>

    
      
      
      


  <li class="md-nav__item">
    <a href="../../api/" title="API Reference" class="md-nav__link">
      API Reference
    </a>
  </li>

    
      
      
      


  <li class="md-nav__item md-nav__item--nested">
    
      <input class="md-toggle md-nav__toggle" data-md-toggle="nav-4" type="checkbox" id="nav-4">
    
    <label class="md-nav__link" for="nav-4">
      FAQ
    </label>
    <nav class="md-nav" data-md-component="collapsible" data-md-level="1">
      <label class="md-nav__title" for="nav-4">
        FAQ
      </label>
      <ul class="md-nav__list" data-md-scrollfix>
        
        
          
          
          


  <li class="md-nav__item">
    <a href="../../FAQ/Audio/" title="Audio" class="md-nav__link">
      Audio
    </a>
  </li>

        
          
          
          


  <li class="md-nav__item">
    <a href="../../FAQ/Autostart/" title="Autostart" class="md-nav__link">
      Autostart
    </a>
  </li>

        
          
          
          


  <li class="md-nav__item">
    <a href="../../FAQ/Motors/" title="Motors" class="md-nav__link">
      Motors
    </a>
  </li>

        
          
          
          


  <li class="md-nav__item">
    <a href="../../FAQ/Network/" title="Network" class="md-nav__link">
      Network
    </a>
  </li>

        
          
          
          


  <li class="md-nav__item">
    <a href="../../FAQ/Miscellaneous/" title="Miscellaneous" class="md-nav__link">
      Miscellaneous
    </a>
  </li>

        
      </ul>
    </nav>
  </li>

    
      
      
      


  <li class="md-nav__item">
    <a href="../" title="Demos" class="md-nav__link">
      Demos
    </a>
  </li>

    
      
      
      


  <li class="md-nav__item">
    <a href="../../examples/" title="Examples" class="md-nav__link">
      Examples
    </a>
  </li>

    
      
      
      


  <li class="md-nav__item">
    <a href="../../author-note/" title="Author's Note" class="md-nav__link">
      Author's Note
    </a>
  </li>

    
  </ul>
</nav>
                  </div>
                </div>
              </div>
            
            
              <div class="md-sidebar md-sidebar--secondary" data-md-component="toc">
                <div class="md-sidebar__scrollwrap">
                  <div class="md-sidebar__inner">
                    
<nav class="md-nav md-nav--secondary">
  
  
    
  
  
    <label class="md-nav__title" for="__toc">Table of contents</label>
    <ul class="md-nav__list" data-md-scrollfix>
      
        <li class="md-nav__item">
  <a href="#video" class="md-nav__link">
    Video
  </a>
  
</li>
      
        <li class="md-nav__item">
  <a href="#setup" class="md-nav__link">
    Setup
  </a>
  
</li>
      
        <li class="md-nav__item">
  <a href="#code" class="md-nav__link">
    Code
  </a>
  
</li>
      
      
      
      
      
    </ul>
  
</nav>
                  </div>
                </div>
              </div>
            
          
          <div class="md-content">
            <article class="md-content__inner md-typeset">
              
                
                
                <h1 id="microphone-voice-activity-direction-detection"><strong>Microphone Voice Activity &amp; Direction Detection</strong></h1>
<div class="admonition question">
<p class="admonition-title">About</p>
<p>This is microphone voice activity and direction detection demo with <a href="http://luxai.com/qtrobot-for-research/#hardware">QTrobot.</a>
Check the video for demonstration. Full code for this demo you can find in our <a href="https://github.com/luxai-qtrobot/tutorials/tree/master/demos/qt_microphone_detection">github tutorial repository.</a></p>
</div>
<h2 id="video"><strong>Video</strong></h2>
<p><a href="http://www.youtube.com/watch?v=ua63cgrO0oU" title="Far Field Microphone Array - Voice Activity &amp; Direction Detection"><img alt="Voice Activity &amp; Direction Detection" src="http://img.youtube.com/vi/ua63cgrO0oU/0.jpg" /></a></p>
<h2 id="setup"><strong>Setup</strong></h2>
<div class="admonition warning">
<p class="admonition-title">NOTICE</p>
<p>You must run this demo on QTRP (head)</p>
</div>
<div class="admonition info">
<p class="admonition-title">How to set it up?</p>
<p>This demo needs some prerequisites:
<div class="codehilite"><pre><span></span><code><span class="err">sudo apt-get update</span>
<span class="err">sudo pip install pyusb click</span>
</code></pre></div>
<em>OR</em>
<div class="codehilite"><pre><span></span><code><span class="err">sudo apt-get update</span>
<span class="err">sudo `which pip` install pyusb click</span>
</code></pre></div></p>
<p>PyUSB needs root privileges, if you run the script without root you will see something like this:
<div class="codehilite"><pre><span></span><code><span class="err">usb.core.USBError: [Errno 13] Access denied (insufficient permissions)</span>
</code></pre></div>
To fix this error we need to set up a udev rule file for the microphone to be able to access it with normal user.
* Create a udev rules file:
<div class="codehilite"><pre><span></span><code><span class="err">ACTION==&quot;add&quot;, SUBSYSTEMS==&quot;usb&quot;, ATTRS{idVendor}==&quot;2886&quot;, ATTRS{idProduct}==&quot;0018&quot;, MODE=&quot;660&quot;, GROUP=&quot;plugdev&quot;</span>
</code></pre></div>
Create this file in folder <em>/etc/udev/rules.d/</em>. For example usual structure of the file name can be <em>Number-Name.rules</em></p>
<ul>
<li>
<p>Add the user to the <em>plugdev</em> group:
<div class="codehilite"><pre><span></span><code><span class="err">adduser username plugdev</span>
</code></pre></div></p>
</li>
<li>
<p>Reload udev system to see your changes:
<div class="codehilite"><pre><span></span><code><span class="err">sudo udevadm control --reload</span>
<span class="err">sudo udevadm trigger</span>
</code></pre></div></p>
</li>
<li>Reboot QTrobot:
<div class="codehilite"><pre><span></span><code><span class="err">sudo reboot</span>
</code></pre></div></li>
</ul>
<p>To run the demo go to <em>qt_microphone_detection</em> folder and run <em>voice_direction</em> python script:
<div class="codehilite"><pre><span></span><code><span class="err">cd qt_microphone_detection/</span>
<span class="err">python voice_direction.py</span>
</code></pre></div>
Now you can speak or sing around the QTrobot and he will follow your voice.</p>
</div>
<h2 id="code"><strong>Code</strong></h2>
<details class="abstract"><summary>Check code here</summary><div class="codehilite"><pre><span></span><code><span class="kn">from</span> <span class="nn">tuning</span> <span class="kn">import</span> <span class="n">Tuning</span>
<span class="kn">import</span> <span class="nn">usb.core</span>
<span class="kn">import</span> <span class="nn">usb.util</span>
<span class="kn">import</span> <span class="nn">time</span>
<span class="kn">import</span> <span class="nn">sys</span>
<span class="kn">import</span> <span class="nn">rospy</span>
<span class="kn">from</span> <span class="nn">std_msgs.msg</span> <span class="kn">import</span> <span class="n">Float64MultiArray</span>

<span class="c1"># microphone orientation</span>
<span class="n">MICROPHONE_ANGLE_OFFSET</span> <span class="o">=</span> <span class="mi">90</span>

<span class="k">if</span> <span class="vm">__name__</span> <span class="o">==</span> <span class="s1">&#39;__main__&#39;</span><span class="p">:</span>

    <span class="n">rospy</span><span class="o">.</span><span class="n">init_node</span><span class="p">(</span><span class="s1">&#39;voice_direction&#39;</span><span class="p">,</span> <span class="n">anonymous</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
    <span class="n">dev</span> <span class="o">=</span> <span class="n">usb</span><span class="o">.</span><span class="n">core</span><span class="o">.</span><span class="n">find</span><span class="p">(</span><span class="n">idVendor</span><span class="o">=</span><span class="mh">0x2886</span><span class="p">,</span> <span class="n">idProduct</span><span class="o">=</span><span class="mh">0x0018</span><span class="p">)</span>
    <span class="k">if</span> <span class="ow">not</span> <span class="n">dev</span><span class="p">:</span>
        <span class="n">rospy</span><span class="o">.</span><span class="n">logerr</span><span class="p">(</span><span class="s2">&quot;Cannot establish connection!&quot;</span><span class="p">)</span>
        <span class="n">sys</span><span class="o">.</span><span class="n">exit</span><span class="p">()</span>

    <span class="n">head_pub</span> <span class="o">=</span> <span class="n">rospy</span><span class="o">.</span><span class="n">Publisher</span><span class="p">(</span><span class="s1">&#39;/qt_robot/head_position/command&#39;</span><span class="p">,</span> <span class="n">Float64MultiArray</span><span class="p">,</span> <span class="n">queue_size</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>

    <span class="c1"># wait for publisher connections</span>
    <span class="n">wtime_begin</span> <span class="o">=</span> <span class="n">rospy</span><span class="o">.</span><span class="n">get_time</span><span class="p">()</span>
    <span class="k">while</span> <span class="p">(</span><span class="n">head_pub</span><span class="o">.</span><span class="n">get_num_connections</span><span class="p">()</span> <span class="o">==</span> <span class="mi">0</span><span class="p">)</span> <span class="p">:</span>
        <span class="n">rospy</span><span class="o">.</span><span class="n">loginfo</span><span class="p">(</span><span class="s2">&quot;waiting for publisher connections...&quot;</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">rospy</span><span class="o">.</span><span class="n">get_time</span><span class="p">()</span> <span class="o">-</span> <span class="n">wtime_begin</span> <span class="o">&gt;</span> <span class="mf">10.0</span><span class="p">:</span>
            <span class="n">rospy</span><span class="o">.</span><span class="n">logerr</span><span class="p">(</span><span class="s2">&quot;Timeout while waiting for publisher connection!&quot;</span><span class="p">)</span>
            <span class="n">sys</span><span class="o">.</span><span class="n">exit</span><span class="p">()</span>
        <span class="n">rospy</span><span class="o">.</span><span class="n">sleep</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>

    <span class="c1"># centering the head</span>
    <span class="n">href</span> <span class="o">=</span> <span class="n">Float64MultiArray</span><span class="p">(</span><span class="n">data</span><span class="o">=</span><span class="p">[</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">])</span>
    <span class="n">head_pub</span><span class="o">.</span><span class="n">publish</span><span class="p">(</span><span class="n">href</span><span class="p">)</span>
    <span class="n">rospy</span><span class="o">.</span><span class="n">sleep</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>

    <span class="n">microphone</span> <span class="o">=</span> <span class="n">Tuning</span><span class="p">(</span><span class="n">dev</span><span class="p">)</span>
    <span class="k">while</span> <span class="ow">not</span> <span class="n">rospy</span><span class="o">.</span><span class="n">is_shutdown</span><span class="p">():</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">microphone</span><span class="o">.</span><span class="n">is_voice</span><span class="p">():</span>
            <span class="k">continue</span>
        <span class="n">mic</span> <span class="o">=</span> <span class="nb">abs</span><span class="p">(</span><span class="n">microphone</span><span class="o">.</span><span class="n">direction</span> <span class="o">-</span> <span class="mi">180</span><span class="p">)</span>
        <span class="n">angle</span> <span class="o">=</span> <span class="n">mic</span> <span class="o">-</span> <span class="n">MICROPHONE_ANGLE_OFFSET</span>
        <span class="n">rospy</span><span class="o">.</span><span class="n">loginfo</span><span class="p">(</span><span class="s2">&quot;mic: </span><span class="si">%d</span><span class="s2"> , head: </span><span class="si">%d</span><span class="s2">&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="n">mic</span><span class="p">,</span> <span class="n">angle</span><span class="p">))</span>
        <span class="n">href</span><span class="o">.</span><span class="n">data</span> <span class="o">=</span> <span class="p">[</span><span class="n">angle</span><span class="p">,</span> <span class="mi">0</span><span class="p">]</span>
        <span class="n">head_pub</span><span class="o">.</span><span class="n">publish</span><span class="p">(</span><span class="n">href</span><span class="p">)</span>

    <span class="n">rospy</span><span class="o">.</span><span class="n">loginfo</span><span class="p">(</span><span class="s2">&quot;shutdowned!&quot;</span><span class="p">)</span>
</code></pre></div>

</details>
                
                  
                
                
                  
                  <hr>
                  <div class="md-source-date">
                    <small>
                      
                        Last update: February 5, 2020
                      
                    </small>
                  </div>
                
              
              
                


              
            </article>
          </div>
        </div>
      </main>
      
        <!--
  Copyright (c) 2016-2020 Martin Donath <martin.donath@squidfunk.com>

  Permission is hereby granted, free of charge, to any person obtaining a copy
  of this software and associated documentation files (the "Software"), to
  deal in the Software without restriction, including without limitation the
  rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  sell copies of the Software, and to permit persons to whom the Software is
  furnished to do so, subject to the following conditions:

  The above copyright notice and this permission notice shall be included in
  all copies or substantial portions of the Software.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  IN THE SOFTWARE.
-->



<!-- Application footer -->
<footer id="footer" class="md-footer">

  <!-- Link to previous and/or next page -->
  

  <!-- Further information -->
  <div class="md-footer-meta md-typeset">
    <div class="md-footer-meta__inner md-grid">
      <!-- Copyright and theme information -->
      <div class="md-footer-copyright">
        
          <div class="md-footer-copyright__highlight">
            &copy; Copyright &copy; 2020 LuxAI S.A.
          </div>
        
      </div>

      <!-- Social links -->
      
  <div class="md-footer-social">
    <link rel="stylesheet" href="../../assets/fonts/font-awesome.css">
    
      <a href="https://github.com/luxai-qtrobot" target="_blank" rel="noopener" title="github" class="md-footer-social__link fa fa-github"></a>
    
      <a href="https://twitter.com/qtrobot" target="_blank" rel="noopener" title="twitter" class="md-footer-social__link fa fa-twitter"></a>
    
      <a href="https://www.linkedin.com/company/luxai/" target="_blank" rel="noopener" title="linkedin" class="md-footer-social__link fa fa-linkedin"></a>
    
      <a href="https://www.facebook.com/luxAI/" target="_blank" rel="noopener" title="facebook" class="md-footer-social__link fa fa-facebook"></a>
    
      <a href="https://www.youtube.com/channel/UCAY0H0HaqQL1GE7lO_iSpbg" target="_blank" rel="noopener" title="youtube" class="md-footer-social__link fa fa-youtube"></a>
    
  </div>

    </div>
  </div>
</footer>
      
    </div>
    
      <script src="../../assets/javascripts/application.c33a9706.js"></script>
      
      <script>app.initialize({version:"1.1",url:{base:"../.."}})</script>
      
        <script src="../../javascripts/extra.js"></script>
      
    
  </body>
</html>